package com.kedacom.demo.rabbitmq_java_client.ch2.workqueues;

import java.util.concurrent.TimeoutException;

import com.rabbitmq.client.AMQP.BasicProperties;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;
import com.rabbitmq.client.MessageProperties;

public class NewTask {

	public static void main(String[] args) throws java.io.IOException, TimeoutException {
		ConnectionFactory factory = new ConnectionFactory();
		factory.setHost(Constants.RABBITMQ_HOST);
		Connection connection = factory.newConnection();
		Channel channel = connection.createChannel();

		final boolean durable = true;
		final BasicProperties messagePersistence = MessageProperties.PERSISTENT_TEXT_PLAIN;
		channel.queueDeclare(Constants.QUEUE_NAME, durable, false, false, null);
		for (int i = 0; i < 30; i++) {
			String message = new SimpleTask("task" + i, i % 2 == 0 ? 1 : 2).toJson();
			channel.basicPublish("", Constants.QUEUE_NAME, messagePersistence, message.getBytes());
			System.out.println(" [x] Sent task " + i);
		}

		channel.close();
		connection.close();
	}

}
